﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using QuickOrm.Utils;

namespace QuickOrm.Helpers
{
    public class AdoDotNetHelper
    {
        /// <summary>
        /// 执行查询(带分页)
        /// </summary>
        /// <param name="factory">工厂</param>
        /// <param name="conn">连接</param>
        /// <param name="sql">查询SQL</param>
        /// <param name="argumentList">参数列表</param>
        /// <param name="conditionArguemntDict">条件字典列表</param>
        /// <param name="sqlSuffix">SQL后缀</param>
        /// <param name="type">返回类型</param>
        /// <param name="pageIndex">页码(从1开始)</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public PagedList<T> executeQuery<T>(DbProviderFactory factory, DbConnection conn, String sql, IList<Object> argumentList, IDictionary<String, Object> conditionArguemntDict, String sqlSuffix, Type type, Int32 pageIndex, Int32 pageSize)
        {
            //String sql = "SELECT * FROM User";
            //System.Data.Common.DbProviderFactory factory = new MySql.Data.MySqlClient.MySqlClientFactory();
            //var conn = factory.CreateConnection();
            //conn.ConnectionString = "Server=192.168.0.221;Port=3306;Database=ssmp_client;Uid=root;Pwd=xinheixia;";
            //conn.Open();

            String sql_COUNT = "SELECT COUNT(*) FROM (" + sql + ") AS TEMP" + DateTime.Now.Ticks;

            var cmd = conn.CreateCommand();
            //得到记录数
            cmd.CommandText = sql_COUNT;
            Int32 recordCount = Convert.ToInt32(cmd.ExecuteScalar());

            //查询数据
            cmd.CommandText = sql;
            //从工厂创建适配器
            System.Data.Common.DbDataAdapter adapter = factory.CreateDataAdapter();
            adapter.SelectCommand = cmd;
            //计算起始记录序号
            int startRowIndex = (pageIndex - 1) * pageSize;
            //填充到DataTable中
            System.Data.DataTable dt = new System.Data.DataTable();
            int dbc = adapter.Fill(startRowIndex, pageSize, dt);

            return null;
        }
    }
}
